<?php

class IdentifikasiTitikModel{
	private $db;
	private $subbagian;
	
	private $validColumn;

	private $data;

	private $result;

	private $sort_by;
	private $sort_order;

	private $search_keyword;
	private $search_field;

	private $limit;
	
	function IdentifikasiTitikModel($dbObject){
		$this->subbagian 	= 'identifikasi_titik';		
		$this->db 			= $dbObject;

		$this->validColumn  = array(		 
				'tgl_penanaman', 
				'no_individu', 
				'no_pancang',
				'replika', 
				'usia_saat_tanam', 
				'cross_asal_pokok', 
				'jenis_tanaman', 
				'kategori_pokok_saat_tanam');	
		
		// settting up configuration
		if(($this->start=$this->getGET('start')) == NULL)
			$this->start = 0;

		$this->sort_by 			= $this->getGET('sort_by');
		$this->sort_order 		= $this->getGET('sort_order');
		$this->search_keyword	= $this->getGET('search_keyword');
		$this->search_field		= $this->getGET('search_field');

		if(($this->limit=$this->getGET('limit')) == NULL)
			$this->limit = 50;		
	}
	
	private function getGET($param){
		if( isset( $_GET[$param] ) && $_GET[$param] != '' )
			return $_GET[$param];
		else
			return NULL;
	}		
	
	public function countAll(){
		$where = "";
		if( $this->search_keyword && $this->search_field )
			$where = "WHERE `".$this->search_field."='".$this->search_keyword."'";
			
		$q = "SELECT COUNT(*) FROM `pokok`.`pancang` $where";
		$this->total_record = $this->db->get_var($q);
		
		return $this->total_record;
	}	
	
	public function search(){			
		$where = "";
		if( $this->search_keyword && $this->search_field )
			$where = "WHERE `biodata_tanaman`.".$this->search_field."='".$this->search_keyword."'";
		
		// get data
		if( $this->sort_by )
			$q = "
			SELECT 
				   `pancang`.`no_individu`,
				   `pancang`.`no_pancang`,
				   `biodata_tanaman`.`id`,
				   `biodata_tanaman`.`tgl_penanaman`,
				   `biodata_tanaman`.`replika`,
				   `biodata_tanaman`.`usia_saat_tanam`,
				   `biodata_tanaman`.`cross_asal_pokok`,
				   `biodata_tanaman`.`jenis_tanaman`,
				   `biodata_tanaman`.`kategori_pokok`
			FROM `pokok`.`biodata_tanaman` INNER JOIN `pokok`.`pancang` on `biodata_tanaman`.`no_individu`=`pancang`.`no_individu`
			$where
			ORDER BY ".$this->sort_by." ".$this->sort_order." LIMIT ".$this->start.",".$this->limit."";
		else
			$q = "
			SELECT 
				   `pancang`.`no_individu`,
				   `pancang`.`no_pancang`,
				   `biodata_tanaman`.`id`,
				   `biodata_tanaman`.`tgl_penanaman`,
				   `biodata_tanaman`.`replika`,
				   `biodata_tanaman`.`usia_saat_tanam`,
				   `biodata_tanaman`.`cross_asal_pokok`,
				   `biodata_tanaman`.`jenis_tanaman`,
				   `biodata_tanaman`.`kategori_pokok`
			FROM `pokok`.`biodata_tanaman` INNER JOIN `pokok`.`pancang` on `biodata_tanaman`.`no_individu`=`pancang`.`no_individu`
			$where
			LIMIT ".$this->start.",".$this->limit."";
		
		$this->result = $this->db->get_results($q, ARRAY_A);
		return $this->result;
	}

	public function getResult(){
		return $this->result;
	}
	
	function put($data){		
		die('Method put() Not applied .. (model.identifikasi_titik)');		
	}	
	
	function isExist(){
		return FALSE; 
	}
}
?>